package com.shop.admin.config;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shop.admin.mapper.*;
import com.shop.admin.service.StyleService;
import com.shop.admin.service.WaresService;
import com.shop.entity.pojo.*;
import com.shop.utils.common.enums.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.List;

@Configuration
@Transactional
public class TimingConfig {

    @Autowired
    private AdminUserMapper adminUserMapper;
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private OrdersDetailMapper ordersDetailMapper;
    @Autowired
    private ImagesMapper imagesMapper;
    @Autowired
    private BrandsMapper brandsMapper;
    @Autowired
    private WaresMapper waresMapper;
    @Autowired
    private StyleMapper styleMapper;
    @Autowired
    private CostMapper costMapper;
    @Autowired
    private WaresService waresService;
    @Autowired
    private StyleService styleService;
    @Autowired
    private RedisTemplate redisTemplate;

//    @Scheduled(cron = "*/10 * * * *  ?")
    @PostConstruct  //项目启动时，同步数据
    public void CheckWares(){
        CheckWaresState();
        CheckStyleState();
    }

    @Async
    public void CheckStyleState(){
        List<Style> styleList = styleService.list();

        for (Style checkStyle : styleList) {

            CheckCostFalse(checkStyle.getId(),checkStyle.getState());
            CheckCostTrue(checkStyle.getId(),checkStyle.getState());
        }
    }

    @Async
    public void CheckCostTrue(Long styleId,Integer state){
        List<Cost> costs = costMapper.selectList(Wrappers.<Cost>lambdaQuery().eq(Cost::getStyleId, styleId).eq(Cost::getState,Message.Put));
        Style style=new Style();
        if (state== Message.Exception&&!CollectionUtils.isEmpty(costs)&&costs.size()>Message.Check_Int_Null) {
            style=style.builder().state(Message.Pull).build();
            int updateStyle = styleMapper.update(style, Wrappers.<Style>lambdaUpdate().eq(Style::getId, styleId));
            if (updateStyle==Message.Check_Int_False) {
                throw new RuntimeException("检查商品款式是否有价格存在报错--有价格");
            }
        }
    }

    @Async
    public void CheckCostFalse(Long styleId, Integer state){
        List<Cost> costs = costMapper.selectList(Wrappers.<Cost>lambdaQuery().eq(Cost::getStyleId, styleId).eq(Cost::getState,Message.Put));
        Style style=new Style();
        if (CollectionUtils.isEmpty(costs)||costs.size()==Message.Check_Int_Null) {
            style=style.builder().state(Message.Exception).build();
            int updateStyle = styleMapper.update(style, Wrappers.<Style>lambdaUpdate().eq(Style::getId, styleId));
            if (updateStyle==Message.Check_Int_False) {
                throw new RuntimeException("检查商品款式是否有价格存在报错--无价格");
            }
        }
    }

    @Async
    public void CheckWaresState(){
        List<Wares> waresList = waresService.list();
        for (Wares checkWares : waresList) {
            CheckWaresFalse(checkWares.getId(),checkWares.getState());
            CheckWaresTrue(checkWares.getId(),checkWares.getState());
        }
    }

    @Async
    public void CheckWaresFalse(Long waresId, Integer state){
        Wares wares=new Wares();
        wares= wares.builder().state(Message.Exception).build();
        List<Style> styles = styleMapper.selectList(Wrappers.<Style>lambdaQuery().eq(Style::getWaresId, waresId).eq(Style::getState,Message.Put));
        List<Cost> costs = costMapper.selectList(Wrappers.<Cost>lambdaQuery().eq(Cost::getWaresId, waresId).eq(Cost::getState,Message.Put));

        if (CollectionUtils.isEmpty(styles)||styles.size()==Message.Check_Int_Null||CollectionUtils.isEmpty(costs)||costs.size()==Message.Check_Int_Null) {
            int updateWares = waresMapper.update(wares, Wrappers.<Wares>lambdaUpdate().eq(Wares::getId, waresId));
            if (updateWares==Message.Check_Int_False) {
                throw new RuntimeException("检查商品是否有款式与价格存在报错");
            }
        }
    }

    @Async
    public void CheckWaresTrue(Long waresId, Integer state){
        Wares wares=new Wares();
        wares= wares.builder().state(Message.Put).build();
        List<Style> styles = styleMapper.selectList(Wrappers.<Style>lambdaQuery().eq(Style::getWaresId, waresId).eq(Style::getState,Message.Put));
        List<Cost> costs = costMapper.selectList(Wrappers.<Cost>lambdaQuery().eq(Cost::getWaresId, waresId).eq(Cost::getState,Message.Put));

        if (state==Message.Exception&&!CollectionUtils.isEmpty(styles)&&styles.size()>Message.Check_Int_Null&&!CollectionUtils.isEmpty(costs)&&costs.size()>Message.Check_Int_Null) {
            int updateWares = waresMapper.update(wares, Wrappers.<Wares>lambdaUpdate().eq(Wares::getId, waresId));
            if (updateWares==Message.Check_Int_False) {
                throw new RuntimeException("检查商品是否有款式与价格存在报错");
            }
        }
    }

}
